package com.test.myestest.controller;


import com.test.myestest.service.ESService;
import com.test.myestest.util.R;
import com.test.myestest.vo.DocumentTest;
import com.test.myestest.vo.ESSearchFileVo;
import com.test.myestest.vo.ESSearchVo;
import com.test.myestest.vo.FileTest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/es")
public class ESController {
    @Resource
    private ESService esService;

    //es数据检索
    @RequestMapping(value = "/search", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R search(ESSearchVo esSearchVo) {
        Map<String, Object> search = esService.search(esSearchVo);
        return R.data(search);
    }

    //es数据添加
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R add(String indexName, DocumentTest documentTest) {
        String add = esService.add(indexName, documentTest);
        return R.ok(add);
    }

    //es数据、文件删除
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R delete(String indexName, String id) {
        String delete = esService.delete(indexName, id);
        return R.ok(delete);
    }

    //es文件添加
    @RequestMapping(value = "/addFile", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R addFile(MultipartFile multipartFile, String importFileUrl, String indexName, FileTest fileTest) {
        String s = esService.addFile(multipartFile, importFileUrl, indexName, fileTest);
        return R.ok(s);
    }

    //es文件检索
    @RequestMapping(value = "/searchFile", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R search(ESSearchFileVo esSearchFileVo) {
        Map<String, Object> search = esService.searchFile(esSearchFileVo);
        return R.data(search);
    }

}
